# 在Python3.0测试通过
# 需要在gitlab里面新建一个AccessToken
# 需要在本地机器设置一个环境变量，比如：GIT_SSH_COMMAND = ssh -i C:/Users/wsq/key/wsq.key

from urllib.request import urlopen
import json
import subprocess,shlex
import os


def main():
    # 获取当前路径
    parentpath = os.getcwd()
    # gitlab地址
    gitlabAddr = 'git.dev.e6gpshk.com'
    # 申请的token
    gitlabToken = 'CynZt3ws9Q6TTDauenuB'
    # gitlab用户名
    username = "zhangyuming"
    # 获取所有group的api组装
    allgroups = urlopen("http://%s/api/v4/groups?private_token=%s" % (gitlabAddr, gitlabToken))
    # 请求
    allgroupsDict = json.loads(allgroups.read().decode())
    for thisgroup in allgroupsDict:
        os.chdir(parentpath)
        thisGroupFullPath = thisgroup['full_path']
        thisGroupId = thisgroup['id']
        print("group full path %s, groupId,%s" % (thisGroupFullPath, thisGroupId))
        # 这里我只clone YM801这个group下的仓库
        if "baixingyaofang" in thisGroupFullPath:
            # 如果路径不存在则创建
            isExists = os.path.exists(parentpath + "/" + thisGroupFullPath)
            if not isExists:
                os.makedirs(parentpath + "/" + thisGroupFullPath)
            # 保持文件目录与远程仓库一直，并切换到目录
            os.chdir(parentpath + "/" + thisGroupFullPath)
            # 获取当前group下的所有仓库, 不用per_page控制每页数量的话, 默认是20个每页, page可以用来控制当前页
            projects = urlopen("http://%s/api/v4/groups/%s/projects?private_token=%s&per_page=100" % (gitlabAddr, str(thisGroupId), gitlabToken))
            projectsDict = json.loads(projects.read().decode())
            print('>>> %s len is %s' % (thisGroupFullPath, len(projectsDict)))
            for thisProject in projectsDict:
                try:
                    # 因为我本地git clone 配置的是http格式的，所以我选择了http_url_to_repo, 如果你是用的git@格式，你就选择ssh_url_to_repo
                    thisProjectURL = thisProject['http_url_to_repo']
                    # 解决git地址不加用户名下载报错问题
                    thisProjectURL = thisProjectURL.replace("//", ("//%s@" % (username)))
                    command = shlex.split('git clone %s' % thisProjectURL)
                    reresultCode = subprocess.Popen(command)
                    print("project url %s" % (thisProjectURL))
                except Exception as e:
                    print("Error on %s:%s" % (thisProjectURL, e.strerror))


if __name__ == '__main__':
    main()
